package com.diycode.controller;

import com.diycode.entity.Admins;
import com.diycode.entity.Users;
import com.diycode.enumPackge.UserTypeEnum;
import com.diycode.service.AdminsService;
import com.diycode.service.UsersService;
import com.diycode.utils.MapControl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {

    @Resource
    UsersService userService;
    @Resource
    AdminsService adminsService;

    //跳转登录页面
    @GetMapping("/")
    public String login(Model model) {
        model.addAttribute("userTypeEnum", UserTypeEnum.values());
        return "login";
    }

    //登录操作
    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> login(String userName, String password, String captcha, Integer type, HttpSession session) {
        //判断用户名、密码、用户类型、验证码是否为空
        if (!StringUtils.hasText(userName) || !StringUtils.hasText(password) || !StringUtils.hasText(captcha) || type == null) {
            return MapControl.getInstance().error("用户名或密码不能为空").getMap();
        }
        //获取系统生成的验证码
        String _captcha = (String) session.getAttribute("captcha");
        //先判断验证码是否正确
        if (!captcha.equalsIgnoreCase(_captcha)) {
            //验证码错误
            return MapControl.getInstance().error("验证码错误").getMap();
        }
        if (UserTypeEnum.getByType(type) == null) {
            return MapControl.getInstance().error("类型不正确").getMap();
        }
        if (UserTypeEnum.Type_1.getType() == type ) {
            Admins user = adminsService.getByLogin(userName, password);
            if (user != null) {
                session.setAttribute("user", user);
                session.setAttribute("type", type);
                return MapControl.getInstance().success().add("data", user).getMap();
            } else {
                return MapControl.getInstance().error("用户名或密码错误").getMap();
            }
        }else if ( UserTypeEnum.Type_2.getType() == type) {
            Users user = userService.getByLogin(userName, password);
            if (user != null) {
                session.setAttribute("user", user);
                session.setAttribute("type", type);
                return MapControl.getInstance().success().add("data", user).getMap();
            } else {
                return MapControl.getInstance().error("用户名或密码错误").getMap();
            }
        }
        return MapControl.getInstance().getMap();
    }

    //退出操作
    @RequestMapping("/logout")
    public String logout(HttpSession session, Model model) {
        //让session失效
        session.invalidate();
        model.addAttribute("userTypeEnum", UserTypeEnum.values());
        //重定向到登录页
        return "login";
    }

}
